package osu.cloudmonitor;

import java.io.BufferedReader;
import java.io.FileReader;

public class LogAnalyzer {
	public static void main(String []args) throws Exception{
		if(args.length != 1){
			System.err.println("java LogAnalyzer <logFile>");
			return;
		}
		
		String logFile = args[0];
		
		BufferedReader reader = new BufferedReader(new FileReader(logFile));
		String line = null;
		int exceptionCount = 0;
		int latCount = 0;
		int minLat = Integer.MAX_VALUE;
		int maxLat = 0;
		int []latencies = new int[65536];
		while((line=reader.readLine())!=null){
			if(line.indexOf("Exception")!=-1){
				exceptionCount ++;
			}
			if(line.indexOf("lat")!=-1){
				latCount++;
				int lat = Integer.parseInt(line.substring(line.indexOf("lat")+4));
				if(lat < minLat)
					minLat = lat;
				if(lat > maxLat)
					maxLat = lat;
				if(lat < latencies.length)
					latencies[lat]++;
				else
					latencies[latencies.length-1]++;
			}
		}
		reader.close();
		System.out.println("Exception count="+exceptionCount);
		System.out.println("Latency count="+latCount);
		System.out.println("Min latency="+minLat);
		System.out.println("Max latency="+maxLat);
		int count = 0;
		for(int i=0; i<latencies.length; i++){
			
			if(count<latCount*99/100 && count + latencies[i] >=latCount*99/100){
				System.out.println("99th latency="+i);
			}
			if(count<latCount*95/100 && count + latencies[i] >=latCount*95/100){
				System.out.println("95th latency="+i);
			}
			if(count<latCount*90/100 && count + latencies[i] >=latCount*90/100){
				System.out.println("90th latency="+i);
			}
			if(count<latCount*50/100 && count + latencies[i] >=latCount*50/100){
				System.out.println("50th latency="+i);
			}
			count += latencies[i];
		}
	}
}
